# 封装邮箱
import os
import yagmail


def send_mail():
    # 配置邮箱（假设使用QQ邮箱，需开启SMTP服务）
    mail = yagmail.SMTP(
        user='2318636512@qq.com',
        password='ajrsnljilowlecdd',
        host='smtp.qq.com',  # QQ邮箱SMTP服务器
        port=465,  # QQ邮箱SSL端口
        smtp_ssl=True  # 启用SSL加密
    )

    subject = 'Test Email from Python (A3mail)'
    body = 'This is a test email sent from a Python script.'
    to = 'norma_bai@126.com'

    # 遍历当前目录下的文件
    for filename in os.listdir('.'):
        if filename.endswith(('.py', '.csv', '.log', '.html', '.json', '.yaml', '.xlsx')):
            file_path = os.path.join('.', filename)

            # 直接传递文件路径给 attachments
            mail.send(
                to=to,
                subject=subject,
                contents=body,
                attachments=file_path  # yapmail 自动处理附件
            )
            print(f'Sent {filename}')

    mail.close()





# 执行发送
if __name__ == '__main__':
    send_mail()




